﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 31.03.2008
 * Zeit: 20:10
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;

namespace Gamadus.Builder.Ast
{
	/// <summary>
	/// Description of VariableName.
	/// </summary>
	public class Named : Expression
	{
		string m_name;
		Named m_left;
		
		public Named(Irony.Compiler.AstNode node, SourceLocation location) : base(node, location)
		{
			switch(node.Term.Name)
			{
				case "Identifier":
					m_left = null;
					m_name = (node as Irony.Compiler.Token).Text;
					break;
				case "QualifiedName":
					throw new NotImplementedException();
				default:
					throw new ArgumentException("Named must be Identifier or QualifiedName");
			}
		}
		
		public override string ToString()
		{
			if(m_left == null)
				return m_name;
			else
				return m_left.ToString()+"."+m_name;
		}
		
		public override string Generate()
		{
			if(m_left == null)
			{
				if(EngineMgr.Instance.Variables.Contains(m_name))
					return "Gml."+m_name;
				else
					return "Variable[\""+m_name+"\"]";
			}
			throw new NotImplementedException();
		}
		
//		protected internal override MSAst.Expression Generate()
//		{
//			MSAst.VariableExpression variable = Runtime.RuntimeMgr.Instance.GetOrMakeGlobal(m_name, typeof(object));
//            return MSAst.Ast.Read(variable);
//        }
//
//        protected internal override MSAst.Expression GenerateAssign(MSAst.Expression right)
//        {
//        	MSAst.VariableExpression variable = Runtime.RuntimeMgr.Instance.GetOrMakeGlobal(m_name, typeof(object));
//            return MSAst.Ast.Assign(
//                variable,
//                MSAst.Ast.ConvertHelper(right, variable.Type)
//            );
//        }
	}
}
